package com.elitecore.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.elitecore.beans.CountryMasterBean;
import com.elitecore.beans.PartnerUser;
import com.elitecore.services.AuthenticateService;

@Controller
@RequestMapping("/checkLogin")
public class LoginController 
{
	private String MODULE = "LoginController";
	private static org.apache.log4j.Logger log	= Logger.getLogger(LoginController.class);
	
	@Autowired
	@Qualifier("AuthenticateServiceImpl")
	private AuthenticateService authenticateService;
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String checklogin(@ModelAttribute("userDetail") PartnerUser uobj, Model model)
	{
		String METHOD = "checklogin";
		log.info(" In "+METHOD+" of "+MODULE);
		String result = "";
		
		try
		{
			if(authenticateService.isRegUser(uobj))
			{
				model.addAttribute("userDerail", uobj);
				result =  "/common/welcome";
			}
			else
			{
				model.addAttribute("userDerail", "0");
				result =  "/common/index";
			}
		}catch(Exception e)
		{
			result =  "/common/error";
			log.info(" Exception in "+METHOD+" of "+MODULE+e,e);
		}
		return result;
	}
	@RequestMapping(value="/TestMe/{loginTest}",method=RequestMethod.GET)
	public String testMethod(@PathVariable String loginTest,Model model,@RequestParam String login)
	{
		String METHOD = "testMethod";
		log.info(" In "+METHOD+" of "+MODULE);
		String result = "";
		log.info("LoginTest = "+loginTest+"  login =="+login);
		result =  "/common/registerUser";
		return result;
	}
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String registerNewUser(Model model)
	{
		String METHOD = "registerNewUser";
		log.info(" In "+METHOD+" of "+MODULE);
		String result = "";
		
		try
		{
			List<CountryMasterBean> countryList = authenticateService.getAllCountry();
			
			if(countryList != null)
			{
				result =  "/common/registerUser";
				model.addAttribute("countryList", countryList);
			}
			else
			{
				result =  "/common/error";
			}
		}catch(Exception e)
		{
			result =  "/common/error";
			log.info(" Exception in "+METHOD+" of "+MODULE+e,e);
		}
		return result;
	}
}